package com.aitoedu.server.service.impl;

import com.aitoedu.server.constant.MessageConstant;
import com.aitoedu.server.dto.LoginDTO;
import com.aitoedu.server.exception.AccountNotFoundException;
import com.aitoedu.server.exception.PasswordErrorException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aitoedu.server.entity.User;
import com.aitoedu.server.service.UserService;
import com.aitoedu.server.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
* @author 曹正豪
* @description 针对表【user(用户名和密码信息)】的数据库操作Service实现
* @createDate 2024-08-09 00:46:59
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        User user = userMapper.getByUsername(username);
        if(user == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(user.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return user;
    }
}




